﻿@{
    Layout = "~/Views/Shared/_Layout_List.cshtml";
}

<div class="fx-content">
    <div class="fx-wrapper">
        <form class="form-inline" id="searchForm">
            <div class="form-group">
                <label>请选择数据库</label>
                <select class="selectpicker" id="linkId" name="linkId" data-style="btn-default btn-sm" data-width="200px">
                </select>
            </div>
            <div class="form-group">
                <button id="buildCode" type="button" class="btn btn-default btn-sm">
                    <i class="glyphicon glyphicon-search"></i>
                    生成代码
                </button>
            </div>
        </form>
    </div>
    <div class="fx-wrapper">
        <table id="dataTable" class="table-bordered"></table>
    </div>
</div>

<script>
    var $table = $('#dataTable');

    $(function () {
        initTable();
        bindEvent();
    });

    //初始化表格
    function initTable() {
        $table.bootstrapTable({
            url: rootUrl + 'Base_SysManage/RapidDevelopment/GetDbTableList',
            idField: 'Id',
            pagination: false,
            method: 'post',
            clickToSelect: true,
            //sidePagination: "server",
            //pageNumber: 1,
            //pageSize: 30,
            //pageList: [10, 30, 60, 100],
            columns: [
                { title: 'ck', field: 'ck', checkbox: true, width: '3%' },
                { title: '表名', field: 'TableName', width: '10%' },
                { title: '描述', field: 'Description', width: '20%' },
                {
                    title: '', field: '_', width: '100%', formatter: function () {
                        return '';
                    }
                }
            ],
            queryParams: function (params) {
                var searchParams = $('#searchForm').getValues();
                $.extend(params, searchParams);

                return params;
            }
        });
    }

    //绑定事件
    function bindEvent() {
        $('#linkId').selectpicker({
            url: '/Base_SysManage/RapidDevelopment/GetAllDbLink',
            textField: 'LinkName',
            valueField: 'Id'
        });
        $('#linkId').on('changed.bs.select', function (e, clickedIndex, newValue, oldValue) {
            var value = $(e.currentTarget).val();
            if (value) {
                $table.bootstrapTable('refresh');
            }
        });

        $('#buildCode').click(function () {
            var tables = [];

            var rows = $table.bootstrapTable('getSelections');
            if (rows.length == 0) {
                dialogError('请选择需要生成的表！');
                return;
            } else {
                $.each(rows, function (index, value) {
                    tables.push(value['TableName']);
                })
            }

            var linkId = $('#linkId').val();
            dialogOpen({
                id: 'form',
                height:'50%',
                title: '生成选项',
                btn: ['生成', '取消'],
                url: rootUrl + 'Base_SysManage/RapidDevelopment/Form?linkId={0}&tables={1}'.format(linkId, encodeURI(JSON.stringify(tables))),
                yes: function (window, body) {
                    window.submitForm();
                }
            });
        });
    }
</script>
